#include "NNetwork.h"


NNetwork::NNetwork(int s) : size(s)
{
	nodeStack = new deque<NodeInfo>();
	init();
}

void NNetwork::init()
{
	int nodedata = 256;
	int dsize = size*(nodedata);
	data = new unsigned char[dsize];

	//fill that shit up
	for(int i = 0;i < dsize;i++)
	{
		data[i] = (rand() % 254) + 1;
	}
	pos = 0;
}

void NNetwork::pushToStack(int node,unsigned char info)
{
	nodeStack->push_back(NodeInfo(node,info));
}

void NNetwork::execute(int n,unsigned char info)
{
	cout << nodeStack->size() << "\n";
	//	cout << "\n" << info;
	int nodedata = 256;

	for(int i = 0;i < 256;i++)
	{
		ifloops[i] = false;
	}
	ifloops[0] = true;
	int ifloop = 0;
	int start = n*nodedata;
	for(int i =  start+1;i < start+nodedata -1;i++)
	{
		//sf::Sleep(sf::Milliseconds(50));
		//cout << info << "\n";
		pos = i;

		int q = data[i] % 100;

		//break ifloop
		if(q > 90 && ifloop > 0)
		{
			ifloops[ifloop] = true;
			ifloop--;
		}
		if(ifloops[ifloop])
		{
			iffy = 0;
			if(q < 3)
			{
				//link next with info
				if(rand() % 100 > 50)
				pushToStack(data[++i],info);
			}
			else
			if(q < 15)
			{
				//add next to info
				info -= data[++i];
			}
			else
			if(q > 25)
			{
				//sub next from info
				info -= data[++i];
			}
			else
			if(q < 35)
			{
				//if next > info
				if(!(data[++i] > info))
				{
					//	cout << "a";
					ifloop++;
					ifloops[ifloop] = false;
				}
			}
			else
			if(q < 45)
			{
				//if next < info
				if(!(data[++i] < info))
				{
					//	cout << "b";
					ifloop++;
					ifloops[ifloop] = false;
				}
			}
			else
			if(q < 55)
			{
				//if next == info
				if(!(data[++i] < info))
				{
					//	cout << "c";
					ifloop++;
					ifloops[ifloop] = false;
				}
				break;
			}
			else
			if(q < 56)
			{
				//write
				data[i-1] = info;
			}
		}
		else
		{
			iffy = 1;
		}

	}
	pos = 0;
}

void NNetwork::run()
{
	for(;;)
	{
		if(!nodeStack->empty())
		{
			NodeInfo * ni = &nodeStack->front();
			execute(ni->n,ni->info);
			nodeStack->pop_front();
		}
	}
}

NNetwork::~NNetwork(void)
{

}
